package cl.kts.dc.pantallas;

import cl.kts.dc.DetencionCiudadana;
import cl.kts.dc.interfaces.Fondo;
import cl.kts.dc.interfaces.Personaje;
import cl.kts.dc.stage.EscNivel1;
import cl.kts.dc.util.Constantes;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;

/**
 *
 * @author Francisco
 */
public class GameScreen implements Screen {

    final DetencionCiudadana game;

    Texture dropImage;
    Texture bucketImage;
    Texture mono;
    TextureRegion currentFrame;
    TextureRegion[] walkFrames;
    TextureRegion[] saltoFrames;

    Animation walkAnimation;
    Animation saltoAnimation;

    Sprite sprite;
    float stateTime;

    EscNivel1 escenario;
   

    Sound dropSound;
    Music rainMusic;
    public OrthographicCamera camera;
    Rectangle bucket;
    Array<Rectangle> raindrops;
    long lastDropTime;
    int dropsGathered;

    public GameScreen(final DetencionCiudadana gam, Personaje personaje) {
        this.game = gam;
  
        /*Texture fondoTexture1 = new Texture(Gdx.files.internal("fondo1.png"));
        fondoTexture1.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
        
        Texture fondoTexture2 = new Texture(Gdx.files.internal("fondo2.png"));
        fondoTexture2.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
        
        Texture fondoTexture3 = new Texture(Gdx.files.internal("fondo3.png"));
        fondoTexture3.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);*/

        Fondo fondo1= new Fondo("fondo1.png", 1,1);
        Fondo fondo2= new Fondo("fondo2.png", 2,2);
        Fondo fondo3= new Fondo("fondo3.png", 3,3);
        Fondo fondo4= new Fondo("fondo4.png", 4,4);
        
        // load the drop sound effect and the rain background "music"
        //dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
        //rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
        //rainMusic.setLooping(true);
        // create the camera and the SpriteBatch
        camera = new OrthographicCamera();
        camera.setToOrtho(false, Constantes.ANCHO_MAXIMO, Constantes.ALTO_MAXIMO);

        escenario = new EscNivel1(personaje,fondo1,fondo2,fondo3,fondo4 );
        Gdx.input.setInputProcessor(escenario);
    }

    @Override
    public void render(float delta) {

        stateTime += Gdx.graphics.getDeltaTime();
        // clear the screen with a dark blue color. The
        // arguments to glClearColor are the red, green
        // blue and alpha component in the range [0,1]
        // of the color to be used to clear the screen.
        Gdx.gl.glClearColor(256f, 256f, 256f, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // tell the camera to update its matrices.
        camera.update();

        // tell the SpriteBatch to render in the
        // coordinate system specified by the camera.
        game.batch.setProjectionMatrix(camera.combined);

        escenario.act(Gdx.graphics.getDeltaTime());
        escenario.draw();

        if (Gdx.input.isKeyPressed(Keys.ESCAPE)) {
            game.setScreen(new Presentacion(game));
            dispose();
        }

    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void show() {
        // start the playback of the background music
        // when the screen is shown
        // rainMusic.play();
    }

    @Override
    public void hide() {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        dropImage.dispose();
        bucketImage.dispose();
        dropSound.dispose();
        rainMusic.dispose();
        escenario.dispose();
    }

}
